package com.ruoyi.web.controller.finance;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.finance.domain.OrderForm;
import com.ruoyi.finance.domain.OrderFormAndOverseasForm;
import com.ruoyi.finance.domain.OverseasForm;
import com.ruoyi.finance.service.IOrderFormAndOverseasFormService;
import com.ruoyi.finance.service.IOrderFormService;
import com.ruoyi.finance.service.IOverseasFormService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/finance/orderForm")
public class OrderFormController {

    @Autowired
    IOrderFormService orderFormService;

    @Autowired
    IOverseasFormService overseasFormService;

    @Autowired
    IOrderFormAndOverseasFormService orderFormAndOverseasFormService;

    @Log(title = "财务导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importExcel")
    public AjaxResult importExcel(MultipartFile file) throws Exception {
        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        int sheetCount = workbook.getNumberOfSheets();

        for (int i = 0; i < sheetCount; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            int fieldLength = sheet.getRow(0).getLastCellNum();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            sheet.getWorkbook().write(outputStream);
            byte[] bytes = outputStream.toByteArray();
            InputStream inputStream = new ByteArrayInputStream(bytes);
            String sheetName;
            if (sheet != null) {
                if (fieldLength == Constants.DECISION) {
                    sheetName = "订单表格";
                    ExcelUtil<OrderForm> util = new ExcelUtil<>(OrderForm.class);
                    List<OrderForm> orderFormList = util.importExcel(sheetName,inputStream,0);
                    orderFormService.insert(orderFormList);
                } else{
                    sheetName = "海外仓操作费表格";
                    ExcelUtil<OverseasForm> util = new ExcelUtil<>(OverseasForm.class);
                    List<OverseasForm> overseasFormList = util.importExcel(sheetName,inputStream,0);
                    overseasFormService.insert(overseasFormList);
                }
            }
        }
        return AjaxResult.success("导入成功");
    }

    @Log(title = "财务导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        List<OrderFormAndOverseasForm> list = orderFormAndOverseasFormService.selectOrderFormAndOverseasForm();
        ExcelUtil<OrderFormAndOverseasForm> util = new ExcelUtil<OrderFormAndOverseasForm>(OrderFormAndOverseasForm.class);
        util.exportExcel(response, list, "用户数据");
    }

}
